using System;
using System.Collections.Generic;
using System.Data;
using System.Xml;
using System.Collections;
using visAd.ExceptionHandler;
using System.IO;
using System.Configuration;


namespace visAd.Providers
{
    public class SCCMProvider : ServerProvider
    {
        string configfilepath = AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["SCCMConfig"];
        private const string mClassName = "SCCMProvider";
        bool _vaIsSupported = new bool();

        #region ProviderBase Implementation

        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            base.Initialize(name, config);
        }

        #endregion

        /// <summary>
        /// Checking for all fields whether supported by SCCM or not.
        /// </summary>
        private bool IsSCCMSupported(XmlDocument FilterXML)
        {
            try
            {
                ArrayList _FieldBuffer = new ArrayList();
                XmlNodeList _xNodeList1;
                XmlNodeList _xNodeList = FilterXML.SelectNodes("/visAd/va-servertype[@type = 'SCCM']/va-filter/va-field");

                //If no Fields will be selected by user then Exception occurs.
                if (_xNodeList.Count == 0)
                {
                    throw new vaCustomException("No fields are Selected.");
                }

                //Loop through each field and verify whether supprorted by SCOM.
                foreach (XmlNode _xFilterXMLNode in _xNodeList)
                {
                    XmlDocument _vaProvidersConfig = new XmlDocument();

                    _vaProvidersConfig.Load(configfilepath);
                    _xNodeList1 = _vaProvidersConfig.SelectNodes("/configuration/va-servertype[@type='SCCM']/va-field");
                    foreach (XmlNode _xAppNode in _xNodeList1)
                    {
                        if (_xFilterXMLNode.Attributes.GetNamedItem("fieldname").Value == _xAppNode.Attributes.GetNamedItem("fieldname").Value)
                        {
                            _FieldBuffer.Add(_xFilterXMLNode.Attributes.GetNamedItem("fieldname").Value.ToString());
                            if (_xFilterXMLNode.HasChildNodes)
                            {
                                vaAttributesCheck(_xFilterXMLNode.Attributes.GetNamedItem("fieldname").Value.ToString(), FilterXML);
                            }
                            break;
                        }
                    }
                }
                if (_xNodeList.Count == _FieldBuffer.Count)
                {
                    _vaIsSupported = true;
                }
                else
                {
                    _vaIsSupported = false;
                    throw new vaCustomException("Not Supported by current version of SCCM.");
                }
            }
            catch (vaCustomException vaEx)
            {
                vaEx.AddTrace(mClassName, "IsSCCMSupported");
                throw vaEx;
            }
            catch (FileNotFoundException ex)
            {
                throw new vaCustomException(ex, mClassName, "IsSCCMSupported");
            }
            catch (Exception ex)
            {
                throw new vaCustomException(ex, mClassName, "IsSCCMSupported");
            }
            //return whether supported or not by SCOM Server.
            return _vaIsSupported;
        }

        /// <summary>
        /// Checking for all field attributes whether supported by SCOM or not.
        /// </summary>
        /// <param name="Field Name"></param>
        /// <param name="FilterXML"></param>
        /// <returns></returns>
        private bool vaAttributesCheck(string vaFieldName, XmlDocument FilterXML)
        {
            try
            {
                XmlNodeList _xNodeList1;
                ArrayList _attributeBuffer = new ArrayList();
                XmlDocument _vaProvidersConfig = new XmlDocument();

                XmlNodeList _xNodeList = FilterXML.SelectNodes("/visAd/va-servertype[@type = 'SCCM']/va-filter/va-field[@fieldname='" + vaFieldName + "']/va-filterattribute");

                //Loop through each field Attributes and verify whether supprorted by SCOM.
                foreach (XmlNode _xFilterXMLNode in _xNodeList)
                {
                    _vaProvidersConfig.Load(configfilepath);
                    _xNodeList1 = _vaProvidersConfig.SelectNodes("/configuration/va-servertype[@type='SCCM']/va-field[@fieldname='" + vaFieldName + "']/va-filterattribute");
                    foreach (XmlNode _xAppNode in _xNodeList1)
                    {
                        if (_xFilterXMLNode.Attributes.GetNamedItem("filtername").Value.ToString() == _xAppNode.Attributes.GetNamedItem("filtername").Value.ToString())
                        {
                            _attributeBuffer.Add(_xFilterXMLNode.Attributes.GetNamedItem("filtername").Value.ToString());
                            break;
                        }
                    }
                }
                if (_xNodeList.Count == _attributeBuffer.Count)
                {
                    _vaIsSupported = true;
                }
                else
                {
                    _vaIsSupported = false;
                    throw new vaCustomException("Not Supported by current version of SCCM.");
                }
            }
            catch (vaCustomException vaEx)
            {
                vaEx.AddTrace(mClassName, "vaAttributesCheck");
                throw vaEx;
            }
            catch (FileNotFoundException ex)
            {
                throw new vaCustomException(ex, mClassName, "vaAttributesCheck");
            }
            catch (Exception ex)
            {
                throw new vaCustomException(ex, mClassName, "vaAttributesCheck");
            }
            //return boolean value i.e. whether supported or not.
            return _vaIsSupported;
        }

        public DataSet GetData(XmlDocument FilterXML)
        {
            
            DataSet _FinalDataset = new DataSet();

            //ArrayList _ServerIDs = new ArrayList();

            //_ServerIDs = getServerInfo(FilterXML);

            try
            {
                //checking whether supported by SCOM or not.

                if (IsSCCMSupported(FilterXML))
                {

                    visAd.Providers.vaSCCMProvider sccmProvider = new visAd.Providers.vaSCCMProvider();

                    _FinalDataset = sccmProvider.getData(FilterXML.OuterXml);//Built-In
                }
                else
                {
                    throw new vaCustomException("Not Supported by current version of SCOM.");
                }

            }
            catch (vaCustomException vaEx)
            {
                vaEx.AddTrace(mClassName, "GetData");
                //throw new vaCustomException("");
                throw vaEx;
            }
            catch (FileNotFoundException ex)
            {
                throw new vaCustomException(ex, mClassName, "GetData", "Configuration File is Missing.");
            }
            catch (Exception ex)
            {
                throw new vaCustomException(ex, mClassName, "GetData");
            }
            //foreach (string serverID in _ServerIDs)
            //{
            //    DataSet SCCMDataSet = sccmProvider.getData(serverID);
            //    _FinalDataset.Merge(SCCMDataSet);
            //}

            return (_FinalDataset);
        }
        public override string GetSchema()
        {
            visAd.Providers.vaSCCMProvider sccmProvider = new visAd.Providers.vaSCCMProvider();
            return sccmProvider.GetSchema();

        }
        public ArrayList getServerInfo(XmlDocument xDoc)
        {
            string strServerType = "SCCM";
            XmlNodeList xnodelist;
            //xnodelist = xDoc.SelectNodes("/visAd/va-serverinfo/server");
            xnodelist = xDoc.SelectNodes("/visAd/va-servertype/va-servers");
            ArrayList _xServerIDs = new ArrayList();
            int i = 0;
            foreach (XmlNode xnode in xnodelist)
            {
                if (xnode.ParentNode.Attributes.GetNamedItem("type").Value == strServerType)
                {
                    _xServerIDs.Insert(i, xnode.Attributes.GetNamedItem("id").Value);
                    i++;
                }
            }
            return _xServerIDs;
        }

    }
}
